<template>
    <div>
       <h2>科室添加</h2>
       <table class="table table-bordered">
        <tbody>
            <tr>
                <td align="right">科室名称</td>
                <td>
                    <input type="text" v-model="query.differentName"/>
                </td>
            </tr>
            <tr>
                <td align="right">科室图片</td>
                <td>
                    <input type="file" @change="Img"/>
                    <img :src="query.differenImg" alt="" width="100px" height="140px" v-if="query.differenImg!=''"/>
                </td>
            </tr>
            <tr>
                <td align="right">成立日期</td>
                <td>
                    <input type="date" v-model="query.createTime"/>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="添加" class="btn btn-primary" @click="ok"/>
                </td>
            </tr>
        </tbody>
       </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { ref,reactive,onMounted } from 'vue';
import { useRoute,useRouter } from 'vue-router';
const router=useRouter();
const route=useRoute();

let query:any=reactive
({
  "differentName": "",
  "differenImg": "",
  "createTime": ""
})


//上传图片
const Img=(e:any)=>
{
    console.log(e);
    let f=e.target.files[0];
    let obj=new FormData();
    obj.append('file',f);
    axios
    ({
        url:'/api/Diff/Img',
        method:'post',
        data:obj
    })
    .then(res=>{
        if(res.data=="不是图片")
        {
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大小大于2M")
        {
            alert("图片不能大于2M");
            return;
        }
        query.differenImg=res.data;
    })
    .catch(err=>{

    })
}


//添加
const ok=()=>
{
    if(query.differentName=="")
    {
        alert("科室名称不能为空");
        return;
    }
    if(query.differenImg=="")
    {
        alert("科室图片不能为空");
        return;
    }
    if(query.createTime=="")
    {
        alert("成立日期不能为空");
        return;
    }
    axios
    ({
        url:'/api/Diff/DifferentAdd',
        method:'post',
        data:query
    })
    .then(res=>{
        if(res.data==-1)
        {
            alert("科室名称不能重复");
            return;
        }
        if(res.data>0)
        {
            alert("添加成功");
            router.push('/diffList');
            return;
        }
        else
        {
            alert("添加失败");
            return;
        }
    })
    .catch(err=>{

    })
}



</script>

<style scoped>

</style>